﻿using UnityEngine;
using System.Collections;

public class SpawnRateController : MonoBehaviour
{
	public int numberOfEnemies;
	public float numberOfSeconds;
	public GameObject enemy;
	public GameObject particles;
	public GameObject enemyGroup;

	private float timeCounter;
	private int enemyCounter;
	public bool isActive;

	void Awake()
	{
		isActive = false;
		timeCounter = 0.0f;
		enemyCounter = 0;
	}

	void Update()
	{
		if(isActive)
		{
			SpawnEnemy ();
		}
	}

	public void SpawnEnemy()
	{
		if ((timeCounter > numberOfSeconds) && (numberOfEnemies > enemyCounter))
		{
			Vector3 pos = transform.position;
			pos.y += 1.0f;
			Instantiate(particles, pos, Quaternion.identity);
			Instantiate(enemy, pos, Quaternion.identity);
			++enemyCounter;
			timeCounter = 0.0f;
		}

		if(enemyCounter >= numberOfEnemies)
		{
			SetActive(false);
		}

		timeCounter += Time.deltaTime;
	}

	public void SetActive(bool val)
	{
		isActive = val;
	}
}
